#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#define BHAddWrite     0x46      //???
#define BHAddRead      0x47      //???
#define BHPowDown      0x00      //????
#define BHPowOn        0x01      //???????? 
#define BHReset        0x07      //?????????BHpowOn?????
#define BHModeH1       0x10      //???? ??1 ?? 11X ???? 120ms 
#define BHModeH2       0x11      //???? ??2 ?? 11X ???? 120ms 
#define BHModeL        0x13      //???? ??4lx ????16ms  
#define BHSigModeH     0x20      //??????????1,????powerdown??
#define BHSigModeH2    0x21      //????
#define BHSigModeL     0x23      //????  


#define SCL_Pin GPIO_Pin_10
#define SDA_Pin GPIO_Pin_11
 

 
void Z_I2C_Init(void)
	{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    
    GPIO_InitTypeDef itd;
    itd.GPIO_Mode=GPIO_Mode_Out_OD;        
    itd.GPIO_Pin=SCL_Pin|SDA_Pin;    
    itd.GPIO_Speed=GPIO_Speed_50MHz;                   
    GPIO_Init(GPIOB,&itd);
 
    GPIO_WriteBit(GPIOA,SCL_Pin,Bit_SET);       //SCL?SDA???????
    GPIO_WriteBit(GPIOA,SDA_Pin,Bit_SET);       //???????????
}
    
void Z_I2C_SetSCL(uint8_t signal)
	{
    if(signal==1) GPIO_WriteBit(GPIOB,SCL_Pin,Bit_SET);
    else GPIO_WriteBit(GPIOB,SCL_Pin,Bit_RESET);
    Delay_us(5);                    //????????,??????
}
 
void Z_I2C_SetSDA(uint8_t signal){
    if(signal==1) GPIO_WriteBit(GPIOB,SDA_Pin,Bit_SET);
    else GPIO_WriteBit(GPIOB,SDA_Pin,Bit_RESET);
    Delay_us(5);
}
 
uint8_t Z_I2C_GetSDA(void){
    return GPIO_ReadInputDataBit(GPIOB,SDA_Pin);
}
 
void Z_I2C_Start(void){
    Z_I2C_SetSDA(1);
    Z_I2C_SetSCL(1);
    Z_I2C_SetSDA(0);
    Z_I2C_SetSCL(0);
}
 
void Z_I2C_End(){
    Z_I2C_SetSDA(0);
    Z_I2C_SetSCL(1);
    Z_I2C_SetSDA(1);
}
 
void Z_I2C_SendByte(uint8_t byte){
    Z_I2C_SetSCL(0);
    for(int i=0;i<8;++i){
        if((byte&0x80)==0) Z_I2C_SetSDA(0);
        else Z_I2C_SetSDA(1);
        byte<<=1;
        Z_I2C_SetSCL(1);
        Z_I2C_SetSCL(0);
    }
}
 
uint8_t Z_I2C_ReveiceByte(){
    uint8_t data=0x00;
    Z_I2C_SetSDA(1);
    for(int i=0;i<8;++i){
        Z_I2C_SetSCL(1);
        if(Z_I2C_GetSDA()==1) data|=(0x80>>i);
        Z_I2C_SetSCL(0);
    }
    return data;
}
 
void Z_I2C_SendACK(uint8_t ack){
    if(ack==0) Z_I2C_SetSDA(0);
    else Z_I2C_SetSDA(1);
    Z_I2C_SetSCL(1);
    Z_I2C_SetSCL(0);
}
 
uint8_t Z_I2C_ReveiceACK(){
    Z_I2C_SetSDA(1);
    Z_I2C_SetSCL(1);
    uint8_t ack=Z_I2C_GetSDA();
    Z_I2C_SetSCL(0);
    return ack;
}
 
uint16_t Z_GY30_GetData(void)
	{
   
    Z_I2C_Start();
    Z_I2C_SendByte(0x46);
    if(Z_I2C_ReveiceACK()!=0) return 0;
    Z_I2C_SendByte(0x01);
    if(Z_I2C_ReveiceACK()!=0) return 0;
    Z_I2C_End();
    
    Z_I2C_Start();
    Z_I2C_SendByte(0x46);
    if(Z_I2C_ReveiceACK()!=0) return 0;
    Z_I2C_SendByte(0x10);
    if(Z_I2C_ReveiceACK()!=0) return 0;
    Z_I2C_End();
    Delay_ms(200);
    
    uint16_t Light=0;
    Z_I2C_Start();
    Z_I2C_SendByte(0x47);
    if(Z_I2C_ReveiceACK()!=0) return 0;
    Light|=Z_I2C_ReveiceByte();
    Light<<=8;
    Z_I2C_SendACK(0);
    Light|=Z_I2C_ReveiceByte();
    Z_I2C_SendACK(1);
    Z_I2C_End();
    
    return Light;
}